﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SMSPanasonic.Business
{
    public class pa_WarrantyStatus
    {
        #region Properties
        public int WarrantyStatusID { get; set; }
        public string WarrantyStatusCode { get; set; }
        public string WarrantyStatusName { get; set; }
        public bool? Disabled { get; set; }
        #endregion

        public pa_WarrantyStatus(IDataReader rd)
        {
            this.WarrantyStatusID = (int)rd["WarrantyStatusID"];
            this.WarrantyStatusCode = (string)rd["WarrantyStatusCode"];
            this.WarrantyStatusName = rd["WarrantyStatusName"].ToString();
            this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
        }

        public static pa_WarrantyStatus GetOne(string statusCode)
        {
            using (var rd = SQLCommand.ExecuteReader(string.Format(@"
                IF(EXISTS(SELECT NULL AS [EMPTY] FROM pa_WarrantyStatus pws WHERE pws.WarrantyStatusCode = '{0}'))
	                SELECT * FROM pa_WarrantyStatus pws WHERE pws.WarrantyStatusCode = '{0}'
                ELSE
                BEGIN
	                INSERT INTO pa_WarrantyStatus
	                (
		                WarrantyStatusCode,
		                WarrantyStatusName,
		                [Disabled]
	                )
	                OUTPUT INSERTED.*
	                VALUES
	                (
		                '{0}',
		                '{0}',
		                0
	                )
                END", statusCode)))
            {
                while (rd.Read())
                {
                    return new pa_WarrantyStatus(rd);
                }
            }
            return null;
        }
    }
}
